﻿namespace ChinaUnicom.Controls
{
    using ChinaUnicom.Models;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;

    public partial class PackageList : UserControl
    {
        public PackageList()
        {
            InitializeComponent();
        }

        Style TitleStyle
        {
            get
            {
                return (Style)Resources["title"];
            }
        }

        Style SubtleStyle
        {
            get
            {
                return (Style)Resources["subtle"];
            }
        }

        ChinaUnicom.Models.ItemCollection items;
        public ChinaUnicom.Models.ItemCollection Items
        {
            get { return items; }
            set
            {
                if (items != value)
                {
                    items = value;
                    this.textContainer.Children.Clear();

                    foreach (Item item in items)
                    {
                        this.textContainer.Children.Add(DrawTitle(item));
                        foreach (Item child in item.Details)
                        {
                            this.textContainer.Children.Add(DrawSubtle(child));
                        }
                    }
                }
            }
        }

        TextBlock DrawTitle(Item item)
        {
            return Draw(item, TitleStyle);
        }

        TextBlock DrawSubtle(Item item)
        {
            return Draw(item, SubtleStyle);
        }

        TextBlock Draw(Item item, Style style)
        {
            TextBlock text = new TextBlock();
            text.Style = style;
            // text.Text = item.Name

            Run name = new Run();
            name.Text = item.Name;

            Run s = new Run();
            s.Text = " : ";

            Run value = new Run();
            value.Text = item.Value;

            text.Inlines.Add(name);
            text.Inlines.Add(s);
            text.Inlines.Add(value);

            return text;
        }
    }
}
